import {handleActions} from 'redux-actions';
import {login, init_login, logout} from "../types/login";
import httpUtil from '../../common/utils/httpUtil';
import userUtil from '../../common/utils/userUtil';

export default handleActions({

  [login](state, action) {
    return httpUtil.myReducers(state, action, function (sucState, sucData) {
      let userInfo = sucData;
      let result = {};
      userUtil.setUserInfo(userInfo);
      result.userInfo = userInfo;
      return result;
    })
  },

  [init_login](state) {
    let userInfo = userUtil.getUserInfo();
    let result = {
      ...state
    };
    if (userInfo) {
      result.userInfo = userInfo;
      result.isLogin = true;
    }
    return result;
  },

  [logout](state) {
    userUtil.setUserInfo(null);
    return {
      ...state,
      isLogin: true,
      userInfo: {}
    }
  }

}, {
  userInfo: {},
  isLogin: false


});




